var oAlert = function (msg, func) {
    iniDialog();
    $("#alert_msg p.content").html(msg);

    $("#alert_msg").dialog("option", "buttons", [{
        text: "OK",
        click: func === undefined ? close : func
    }]);

    $("#alert_msg").dialog("open").closest(".ui-dialog").addClass("font-size", "1.2em");

    function iniDialog() {
        if ($("#alert_msg").length == 0) {
            $("body").append("<div id='alert_msg'><p class='content'></p></div>");
            $("#alert_msg").dialog({
                modal: true,
                title: "warnning",
                autoOpen: false,
                buttons: [{ text: "OK", click: function () { $("#alert_msg").dialog("close"); } }]
            }).closest(".ui-dialog").css("font-size", "1.2em"); ;
        }
    }

    function close() {
        $("#alert_msg").dialog("close");
    }
};;
var enableProgressBar = true;
var processTimeTick, endAjax = false;

$(document).ready(function () {
    iniCSSFormInIE6();
    if ($.easyValidate) {
        var setting = "<div class=\"ui-state-error ui-corner-all alertPanel\"><span class=\"ui-icon ui-icon-alert alertIcon\"></span><span class=\"alertText\"></span></div>";
        $.easyValidate({
            summary: true,
            onError: function (message) {
                var a = $(this).next("div.alertPanel");
                if (a.length == 0) {
                    a = $(this).after(setting).next();
                }

                $(".alertText", $(a).addClass("ui-state-error")).text(message.join("<br>"));
                $(a).children("span").addClass("ui-icon-alert").removeClass("ui-icon-check");

            },
            errorEvent: false,
            onCorrect: function () {
                if ($(this).val().length == 0) {
                    return;
                }
             
                var a = $(this).next("div.alertPanel");
                if (a.length == 0) {
                    a = $(this).after(setting).next();
                }
                $(".alertText", $(a).removeClass("ui-state-error")).text("");
                $(a).children("span").removeClass("ui-icon-alert").addClass("ui-icon-check");
            }
        });
    }
    /// customer alert box.
    $("#ajaxErrorMessage").ajaxError(function (event, request) {
        $(this).html(request.responseText).dialog('open');
    }).dialog({ title: "出错", height: 450, width: 600, modal: true, autoOpen: false,
        buttons: { "确认": function () { $(this).dialog("close"); } }
    });
    $(".iconContainer", $("#topMenuBar")).hover(
					function () { $(this).addClass('ui-state-highlight'); },
					function () { $(this).removeClass('ui-state-highlight'); }
				);
    $("#progressbar").ajaxStart(
                function () {
                    if (enableProgressBar) {
                        endAjax = false;
                        processTimeTick = setTimeout(function () { if (!endAjax) { $("#progressbar").show(); } }, 1000);
                    }
                }
            ).ajaxStop(function () {
                if (enableProgressBar) {
                    endAjax = true;
                    $(this).hide();
                }
            });
});



function iniCSSFormInIE6() {
    if ($.browser.version === "6.0") {
        $(".cssform input[type='text'], .cssform input[type='password'], .cssform textarea")
        .addClass("cssform-input")
        .focus(function () { $(this).addClass("cssform-input-focus"); }).blur(
        function () { $(this).removeClass("cssform-input-focus"); }
        );
    }
};
function hasPermission(intTotal, intOperator) {
    if (typeof intOperator == 'string')
        intOperator = parseInt(intOperator);
    if (typeof intOperator == 'string')
        intTotal = parseInt(intTotal);
    return intOperator != 0 && intTotal >= intOperator && (intTotal & intOperator) == intOperator;
};
//slideMenu
(function ($) {

    /*********************
    //* jQuery Multi Level CSS Menu #2- By Dynamic Drive: http://www.dynamicdrive.com/
    //* Last update: Nov 7th, 08': Limit # of queued animations to minmize animation stuttering
    //* Menu avaiable at DD CSS Library: http://www.dynamicdrive.com/style/
    *********************/

    //Specify full URL to down and right arrow images (23 is padding-right to add to top level LIs with drop downs):
    var arrowimages = { down: ['downarrowclass', '/content/images/down.gif', 23], right: ['rightarrowclass', '/content/images/right.gif'] }

    var jqueryslidemenu = {

        animateduration: { over: 200, out: 100 }, //duration of slide in/ out animation, in milliseconds

        buildmenu: function (menuid, arrowsvar) {
            jQuery(document).ready(function ($) {
                var $mainmenu = $("#" + menuid + ">ul")
                var $headers = $mainmenu.find("ul").parent()
                $headers.each(function (i) {
                    var $curobj = $(this)
                    var $subul = $(this).find('ul:eq(0)')
                    this._dimensions = { w: this.offsetWidth, h: this.offsetHeight, subulw: $subul.outerWidth(), subulh: $subul.outerHeight() }
                    this.istopheader = $curobj.parents("ul").length == 1 ? true : false
                    $subul.css({ top: this.istopheader ? this._dimensions.h + "px" : 0 })
                    $curobj.children("a:eq(0)").css(this.istopheader ? { paddingRight: arrowsvar.down[2] } : { }).append(
                        '<img src="' + (this.istopheader ? arrowsvar.down[1] : arrowsvar.right[1])
                            + '" class="' + (this.istopheader ? arrowsvar.down[0] : arrowsvar.right[0])
                                + '" style="border:0;" />'
			);
                    $curobj.hover(
                        function (e) {
                            var $targetul = $(this).children("ul:eq(0)")
                            this._offsets = { left: $(this).offset().left, top: $(this).offset().top }
                            var menuleft = this.istopheader ? 0 : this._dimensions.w
                            menuleft = (this._offsets.left + menuleft + this._dimensions.subulw > $(window).width()) ? (this.istopheader ? -this._dimensions.subulw + this._dimensions.w : -this._dimensions.w) : menuleft
                            if ($targetul.queue().length <= 1) //if 1 or less queued animations
                                $targetul.css({ left: menuleft + "px", width: this._dimensions.subulw + 'px' }).slideDown(jqueryslidemenu.animateduration.over)
                        },
                        function (e) {
                            var $targetul = $(this).children("ul:eq(0)")
                            $targetul.slideUp(jqueryslidemenu.animateduration.out)
                        }
			); //end hover
                }) //end $headers.each()
                $mainmenu.find("ul").css({ display: 'none', visibility: 'visible' })
            }); //end document.ready
        }
    };

    //build menu with ID="myslidemenu" on page:
    jqueryslidemenu.buildmenu("mainMenu", arrowimages)

})($);;
//Sample: String.format("I am {0}, you are {1}","a","b");
String.format = function () {
    if (arguments.length == 0)
        return null;

    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
};


//string To Date
String.toDate = function (fmt, str) {
    var patten = /(y|M|d|h|m|s)/g;
    var result = { y: 0, M: 0, d: 0, h: 0, m: 0, s: 0 };
    var r = patten.exec(fmt);
    var currentChar = r[0];
    while (r != null) {
        var a = str.substr(r.index, 1);
        if (currentChar != r[0]) {
            currentChar = r[0];
        }
        result[currentChar] = (result[currentChar] * 10) + parseInt(a);
        r = patten.exec(fmt);
    }
    return new Date(result.y, result.M, result.d, result.h, result.m, result.s);
};;
function $import(path, type) {
    var i,
        base,
        src = "global.js",
        scripts = document.getElementsByTagName("script");

    for (i = 0; i < scripts.length; i++) {
        if (scripts[i].src.match(src)) {
            base = scripts[i].src.replace(src, "");
            break;
        }
    }

    if (type == "css") {
        document.write("<" + "link href=\"" + base + path + "\" rel=\"stylesheet\" type=\"text/css\"></" + "link>");
    } else {
        document.write("<" + "script src=\"" + base + path + "\"></" + "script>");
    }
};
